package com.xhwl.centiotdata.suppliersystem.aolai;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.ApplicationException;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffDTO;
import com.xhwl.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author： wangyiling
 * @Description：奥莱主数据 基本请求
 * @Date 2022/9/27
 */
@Service
public class GeneralAoLaiHandler {
    private static final Logger log = LoggerFactory.getLogger(GeneralAoLaiHandler.class);

    public List<StaffDTO> post(String api, Map<String, Object> param, String ipAndPort, String appId, String token) {
        String result = null;
        StringBuffer pathBuffer = new StringBuffer()
                .append(ipAndPort)
                .append(api);
        try {
            Map<String, String> header = new HashMap<>();
            header.put("appToken", token);
            header.put("appId", appId);
            result = HttpRequest
                    .post(pathBuffer.toString())
                    .addHeaders(header)
                    .body(JSON.toJSONString(param))
                    .execute()
                    .body();
        } catch (HttpException e) {
            log.error("【aolai post】path:{};params:{}; request failed!", pathBuffer, JSON.toJSONString(param));
            e.printStackTrace();
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)) {
            log.error("【aolai post】path:{};params:{};result is null!", pathBuffer, JSON.toJSONString(param));
            throw new BusinessException();
        }
        log.error("【aolai post】path:{};params:{}; 执行成功!", pathBuffer, JSON.toJSONString(param));

        return  JSONObject.parseArray(JSON.parseObject(result).getString("result"), StaffDTO.class);
    }
}
